package tos

import (
	"context"
	"tosServer/internal/pkg/common"

	"github.com/gin-gonic/gin"
	"github.com/volcengine/ve-tos-golang-sdk/v2/tos"
)

type symlinkParams struct {
	Ak           string `json:"ak" binding:"required"`
	Sk           string `json:"sk" binding:"required"`
	Endpoint     string `json:"endpoint" binding:"required"`
	Region       string `json:"region" binding:"required"`
	Bucket       string `json:"bucket" binding:"required"`
	Key          string `json:"key" binding:"required"`
	TargetBucket string `json:"target_bucket" binding:"required"`
	TargetKey    string `json:"target_key" binding:"required"`
}

/**
 * 创建软连接
 */
func (h *Object) Symlink(c *gin.Context) {
	params := &symlinkParams{}
	if err := c.ShouldBind(params); err != nil {
		h.Api.Error(c, &common.R{
			Msg: err.Error(),
		})
		return
	}

	credential := tos.NewStaticCredentials(params.Ak, params.Sk)
	client, err := tos.NewClientV2(params.Endpoint, tos.WithCredentials(credential), tos.WithRegion(params.Region))
	if err != nil {
		h.Api.Error(c, &common.R{
			Msg: err.Error(),
		})
		return
	}
	defer client.Close()
	output, err := client.PutSymlink(context.Background(), &tos.PutSymlinkInput{
		Bucket:              params.Bucket,
		Key:                 params.Key,
		SymlinkTargetKey:    params.TargetKey,
		SymlinkTargetBucket: params.TargetBucket,
	})

	if err != nil {
		h.Api.Error(c, &common.R{
			Msg: err.Error(),
		})
		return
	}

	h.Api.Success(c, &common.R{
		Data: output,
	})
}
